package com.role{
	import com.collection.ConfigCollection;
	import com.utils.Collide;

	/**
	 * 创建者：薛恒杰
	 * 工程名称：Marbles
	 * 创建时间：Jul 31, 2012 1:03:41 PM
	 * 类说明：MoveBase.as,
	 */
	public class MoveBase extends CycleBase{
		public var quality:Number;//质量
		///移动参数
		protected var angle:Number;//角度
		
		protected var velocity:Number;//初始速度
		public var vx:Number;
		public var vy:Number;
		protected var acceleration:Number;//加速度
		protected var ax:Number;//加速度x
		protected var ay:Number;//加速度y
		public var bounce:Number;
		protected var direction:int;
		protected var ev:Number;//当前速度
		public function MoveBase(life:int, attack:int, defense:int, quality:Number, radius:int = 10, vScale:Number = 1, acceleration:Number = -.1, bounce:Number = -.1){
			super(life, attack, defense, radius);
			this.quality = quality;
			this.velocity = vScale * ConfigCollection.BaseVelocity;
			this.acceleration = acceleration;
			this.bounce = bounce;
			stop();
		}
		/**
		 * 获取即时碰撞系数。
		 */
		override public function get collide():Collide{
			_collide.velocity = ev;
			return _collide;
		}
		/**
		 * 执行移动操作，一般在fire方法设置参数后启动。
		 * @return 返回true则移动上为结束，否则结束。
		 */
		public function execution():Boolean{
			ev += acceleration;
			if(ev > 0){
				vx += ax;
				vy += ay;
				x += vx;
				y += vy;
				return true;
			} else {
				stop();
				return false;
			}
		}
		public function fireScale(angle:Number, vScale:Number):void{
			angle = angle * Math.PI / 180
			fire(angle, this.velocity * vScale);
		}
		/**
		 * 移动前的参数设置。
		 * @param angle 要移动的角度
		 * @param speed 移动注入的速度比例
		 */
		public function fire(angle:Number, v:Number):void{
			ev = v;
			this.angle = angle;
			var cosa:Number = Math.cos(angle);
			var sina:Number = Math.sin(angle);
			ax = acceleration * cosa;
			ay = acceleration * sina;
			vx = ev * cosa;
			vy = ev * sina;
			
			var radians:Number = Math.atan2(vy, vx) * 180 / Math.PI;
			var _direction:int = Math.round(radians / 45);
			if (radians < 0){//角度为负z
				_direction = Math.abs(_direction + 8);
			}
			//转成跟图片一致的
			switch (_direction){
				case 0:direction = 2;break;//右
				case 1:direction = 5;break;//右下
				case 2:direction = 0;break;//下
				case 3:direction = 4;break;//左下
				case 4:direction = 1;break;//左
				case 5:direction = 6;break;//左上
				case 6:direction = 3;break;//上
				case 7:
				case 8:direction = 7;break;//右上
				default:
			}
			
			drawRectangle(64, direction * 64, 64, 64);
		}
		
		public function stop():void{
			ev = 0;
			ay = 0;
			ax = 0;
			vx = 0;
			vy = 0;
			drawRectangle(0, direction * 64, 64, 64);
		}
	}
}